package cn.ffcs.uec.outStaff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffPsnorgEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecPsnorg;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecOutStaffPsnorgEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecOutStaffPsnorgEditBean bean = new UecOutStaffPsnorgEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecStaffInfo;
	/**
	 * 
	 */
	private UecPsnorg uecPsnorg;

	/**
	 * 组织
	 */
	private UecOrgOrgs uecOrgOrgs;

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);

		this.bean.getUecOrgOrgsBandboxExt().addForward(
				"onUecOrgOrgsEditSelected", comp,
				"onUecOrgOrgsEditSelectedResponse");
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecPsnorgEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		List<NodeVo> nodeVoIsList = new ArrayList<NodeVo>();
		String[][] is = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < is.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(is[i][j]);
				nodeVo.setName(is[i][j + 1]);
			}
			nodeVoIsList.add(nodeVo);
		}
		// 是否终止
		ListboxUtils.rendererForEdit(bean.getEndflag(), nodeVoIsList);
		// 是否转入人员档案
		ListboxUtils.rendererForEdit(bean.getIndocflag(), nodeVoIsList);
		// 是否核心员工
		ListboxUtils.rendererForEdit(bean.getOrgglbdef6(), nodeVoIsList);

		// 人员类型
		List<NodeVo> nodeVos = new ArrayList<NodeVo>();
		String[][] type = { { "0", "新进" }, { "1", "变更" }, { "3", "退出" } };
		for (int i = 0; i < type.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(type[i][j]);
				nodeVo.setName(type[i][j + 1]);
			}
			nodeVos.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPsntype(), nodeVos);
	}

	/**
	 * 监听UecOrgOrgsBandboxExt传出的事件
	 * 
	 * @param event
	 */
	public void onUecOrgOrgsEditSelectedResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			uecOrgOrgs = (UecOrgOrgs) event.getOrigin().getData();
		}
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecPsnorgEditWindow().setTitle("新增组织关系");
			bean.getUecPsnorgEditDiv().setHeight("30%");
			uecStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkHrorg())) {
				UecOrgOrgs uecOrgOrgs = new UecOrgOrgs();
				uecOrgOrgs.setPkOrg(uecStaffInfo.getPkHrorg());
				uecOrgOrgs = uecOrgOrgsManager
						.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
				if (null != uecOrgOrgs) {
					bean.getUecOrgOrgsBandboxExt().setValue(
							uecOrgOrgs.getName());
				}
			}
		}
		if ("view".equals(opType)) {
			this.bean.getUecPsnorgEditWindow().setTitle("查看组织关系");
			uecPsnorg = (UecPsnorg) arg.get("uecPsnorg");
			if (null != uecPsnorg) {
				PubUtil.fillBeanFromPo(uecPsnorg, bean);
				stringTransToDate();
				setOrgBandBoxValue();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecPsnorgEditDiv(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecPsnorgEditWindow().setTitle("编辑组织关系");
			bean.getUecPsnorgEditDiv().setHeight("30%");
			uecPsnorg = (UecPsnorg) arg.get("uecPsnorg");
			if (null != uecPsnorg) {
				PubUtil.fillBeanFromPo(uecPsnorg, bean);
				setOrgBandBoxValue();
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		String currentName = PlatformUtil.getCurrentUser().getScreenName();
		if ("add".equals(opType)) {
			uecStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
			uecPsnorg = UecPsnorg.newInstance();
			PubUtil.fillPoFromBean(bean, uecPsnorg);
			if (uecOrgOrgs != null) {
				uecPsnorg.setPkHrorg(uecOrgOrgs.getPkOrg());
			} else {
				uecPsnorg.setPkHrorg(uecStaffInfo.getPkHrorg());
			}
			dateTransToString();
			uecPsnorg.setStaffId(uecStaffInfo.getOutStaffId());
			uecPsnorg.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecPsnorg.setPkPsndoc(uecStaffInfo.getPkPsnMsg());
			uecPsnorg.setDr(1L);
			// 创建人
			uecPsnorg.setCreator(currentName);
			// 创建时间
			uecPsnorg.setCreationtime(DateUtil.getCurrenDate());
			uecPsnorg.addOnly();
			Events.postEvent("onOK", this.self, uecPsnorg);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecPsnorg);
			dateTransToString();
			if (uecOrgOrgs != null) {
				uecPsnorg.setPkHrorg(uecOrgOrgs.getPkOrg());
			}
			// 修改人
			uecPsnorg.setModifier(currentName);
			// 修改时间
			uecPsnorg.setModifiedtime(DateUtil.getCurrenDate());
			uecPsnorg.updateOnly();
			Events.postEvent("onOK", this.self, uecPsnorg);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecPsnorgEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		/*
		 * if (bean.getLastflag().getValue().length() > 1) { return
		 * "最近记录标识只能一个字符长度！";
		 */
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourType().getValue())) {
		// return "填写奖励类型";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourMeas().getValue())) {
		// return "填写奖励措施";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourOrg().getValue())) {
		// return "填写奖励机构";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourRank().getValue())) {
		// return "填写奖励级别";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourGrade().getValue())) {
		// return "填写奖励档次";
		// }
		if (StrUtil.isNullOrEmpty(bean.getUecOrgOrgsBandboxExt().getValue())) {
			return "请选择人力资源组织";
		}else if (StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			return "填选择进入日期";
		}else if (StrUtil.isNullOrEmpty(bean.getJoinsysdate().getValue())) {
			return "填选择集团日期";
		}
		return null;
	}

	/**
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			uecPsnorg.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			uecPsnorg
					.setEnddate(formatter.format(bean.getEnddate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getJoinsysdate().getValue())) {
			uecPsnorg.setJoinsysdate(formatter.format(bean.getJoinsysdate()
					.getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 奖励日期
		if (null != uecPsnorg.getBegindate()) {
			bean.getBegindate().setValue(
					DateUtil.patternStrToDate(uecPsnorg.getBegindate()));
		}
		if (null != uecPsnorg.getEnddate()) {
			bean.getEnddate().setValue(
					DateUtil.patternStrToDate(uecPsnorg.getEnddate()));
		}
		if (null != uecPsnorg.getJoinsysdate()) {
			bean.getJoinsysdate().setValue(
					DateUtil.patternStrToDate(uecPsnorg.getJoinsysdate()));
		}
	}
	
	/**
	 * 页面人力资源组织orgBandBox设置值
	 */
	private void setOrgBandBoxValue() {
		if (!StrUtil.isNullOrEmpty(uecPsnorg.getPkHrorg())) {
			UecOrgOrgs uecOrgOrgs = new UecOrgOrgs();
			uecOrgOrgs.setPkOrg(uecPsnorg.getPkHrorg());
			uecOrgOrgs = uecOrgOrgsManager
					.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
			if (null != uecOrgOrgs) {
				bean.getUecOrgOrgsBandboxExt().setValue(
						uecOrgOrgs.getName());
			}
		}
	}

}
